var express = require('express');
var router = express.Router();
const { articleModel } = require('../db/index')
const Op = require('sequelize').Op

/* GET users listing. */
router.post('/get', async (req, res, next) => {
  const { page = 1, pageSize = 10 } = req.body
  if (page < 1) {
    page = 1
  }
  const users = await articleModel.findAll({
    limit: pageSize,
    offset: (page - 1) * pageSize,
  })
  const total = await articleModel.findAll()
  res.status(200).json({
    code: 200,
    data: users,
    total: total.length
  })
});
router.post("/", async function (req, res, next) {
  const rs = await articleModel.create(req.body)
  res.json({
    code: 200
  })
});
router.post("/update/:id", async function (req, res, next) {
  const whereOption = {}
  if (req.params.id) {
    whereOption.article_id = req.params.id
  }
  const result = await articleModel.update(req.body, {
    where: whereOption
  })
  res.json({
    code: 200
  })
});
router.post("/delete/:id", async function (req, res, next) {
  const result = await articleModel.destroy({
    where: {
      article_id: req.params.id
    }
  })
  res.json({
    code: 200
  })
});

module.exports = router;
